package com.sl.hx.shop.ui.classify;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.sl.hx.shop.R;
import com.sl.hx.shop.base.BaseFragment;
import com.sl.hx.shop.bean.GoodsClassify;
import com.sl.hx.shop.ui.classify.contacts.ClassifyContacts;
import com.sl.hx.shop.ui.classify.fragment.ClassifyListFragment;
import com.sl.hx.shop.ui.classify.presenter.ClassifyPresenter;
import com.sl.hx.shop.ui.home.activity.SearchActivity;
import com.sl.hx.shop.ui.home.adapter.BaseFragmentAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import q.rorbin.verticaltablayout.VerticalTabLayout;
import q.rorbin.verticaltablayout.adapter.TabAdapter;
import q.rorbin.verticaltablayout.widget.ITabView;
import q.rorbin.verticaltablayout.widget.TabView;

public class ClassifyFragment extends BaseFragment<ClassifyContacts.view, ClassifyPresenter> implements ClassifyContacts.view {

    @BindView(R.id.home_search)
    ImageView homeSearch;
    @BindView(R.id.classify_tablayout)
    VerticalTabLayout tabLayout;
    @BindView(R.id.classify_viewpage)
    ViewPager viewpager;

    private BaseFragmentAdapter mFragmentAdapter;

    @Override
    protected ClassifyPresenter createPresenter() {
        return new ClassifyPresenter(mContext);
    }

    @Override
    protected int getViewLayoutId() {
        return R.layout.fragment_classify;
    }

    @Override
    protected void initView(View mView) {
        //请求数据
        mPresenter.getCate();
    }

    @Override
    public void ShowCate(final List<GoodsClassify> goodsClassifyList) {
        if (goodsClassifyList != null) {
            final List<String> mNameList = new ArrayList<>();
            List<Fragment> mFragmentList = new ArrayList<>();
            for (int i = 0; i < goodsClassifyList.size(); i++) {
                mNameList.add(goodsClassifyList.get(i).getName());
                mFragmentList.add(createListFragments(goodsClassifyList.get(i)));
            }
            if (mFragmentAdapter == null) {
                mFragmentAdapter = new BaseFragmentAdapter(getChildFragmentManager(), mFragmentList, mNameList);
            } else {
                //刷新fragment
                mFragmentAdapter.setFragments(getChildFragmentManager(), mFragmentList, mNameList);
            }
            viewpager.setAdapter(mFragmentAdapter);
            tabLayout.setupWithViewPager(viewpager);
            tabLayout.setTabAdapter(new TabAdapter() {
                @Override
                public int getCount() {
                    return mNameList.size();
                }

                @Override
                public ITabView.TabBadge getBadge(int position) {
                    return null;
                }

                @Override
                public ITabView.TabIcon getIcon(int position) {
                    return null;
                }

                @Override
                public ITabView.TabTitle getTitle(int position) {
                    return new TabView.TabTitle.Builder()
                            .setContent(mNameList.get(position))
                            .setTextColor(0xFFF43142, 0xFF999999)
                            .build();
                }

                @Override
                public int getBackground(int position) {
                    return 0;
                }
            });
        }
    }

    private ClassifyListFragment createListFragments(GoodsClassify goodsClassify) {
        ClassifyListFragment fragment = new ClassifyListFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable("goodsClassify", goodsClassify);
        fragment.setArguments(bundle);
        return fragment;
    }



    @OnClick({R.id.rl_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_search:
                //跳转搜索页
                toActivity(SearchActivity.class);
                break;
        }
    }
}
